package com.bhs.sansonglogistics.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.bean.SourceGoodsDataBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.CenterPopupView;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 报价信息
 */
public class QuotedPriceDialog extends CenterPopupView implements NetCallBack {

    private String quotedPrice;//报价
    private int quotationTime;//报价时间
    private String bid;//货主出价
    private String phone;//电话
    private TextView mTvBid;
    private TextView mTvPhone;
    private EditText mTvQuotedPrice;
    private TextView mTvRemark;
    private TextView mTvQuotationTime;
    private Button mBtnSubmit;
    private BaseFragment baseFragment;
    private BaseActivity baseActivity;
    private MyClickListener listener;
    private String deliver_sn;

    private OrderGoodsBean info;

    public QuotedPriceDialog(@NonNull BaseFragment context, BaseActivity baseActivity, OrderGoodsBean info) {
        super(context != null ? context.getContext() : baseActivity.mContext);
        baseFragment = context;
        this.baseActivity = baseActivity;
        this.info = info;
        this.bid = info.getLast_price();
        this.quotedPrice = info.getQuoted_price();
        this.quotationTime = info.getQuoted_create_time();
        this.deliver_sn = info.getDeliver_sn();
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
        mTvBid.setText(String.format("%s元", bid));
        if (!TextUtils.isEmpty(quotedPrice) && !quotedPrice.equals("0")) {
            mTvQuotationTime.setVisibility(VISIBLE);
            mTvQuotationTime.setText(DateUtils.getTime(quotationTime));
            mTvQuotedPrice.setText(quotedPrice);
            mTvQuotedPrice.setEnabled(false);
            mBtnSubmit.setVisibility(GONE);
        } else {
            mTvQuotationTime.setVisibility(GONE);
            mTvQuotedPrice.setEnabled(true);
            mBtnSubmit.setVisibility(VISIBLE);
        }
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_quoted_price;
    }


    // 设置最大宽度，看需要而定，
    @Override
    protected int getMaxWidth() {
        return MyUtils.getScreenWidth(getContext()) * 11 / 12;
    }

    private void initView() {
        mTvBid = findViewById(R.id.tv_bid);
        mTvPhone = findViewById(R.id.tv_phone);
        mTvQuotedPrice = findViewById(R.id.tv_quoted_price);
        mTvRemark = findViewById(R.id.tv_remark);
        mTvQuotationTime = findViewById(R.id.tv_quotation_time);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mTvPhone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSelect();
            }
        });
        mBtnSubmit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(mTvQuotedPrice.getText())) {
                    ToastUtil.show("请输入您的报价金额");
                    return;
                }
                if (Double.parseDouble(mTvQuotedPrice.getText().toString())<=Double.parseDouble(bid)) {
                    ToastUtil.show("您的报价金额必须大于货主出价");
                    return;
                }
                if (baseFragment != null) {
                    baseFragment.networkRequest(baseFragment.netApi.quotation(deliver_sn, mTvQuotedPrice.getText().toString()), QuotedPriceDialog.this);
                } else {
                    baseActivity.networkRequest(baseActivity.netApi.quotation(deliver_sn, mTvQuotedPrice.getText().toString()), QuotedPriceDialog.this);
                }
            }
        });
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean dataBean = new Gson().fromJson(json, BaseBean.class);
        if (dataBean.isStatus()) {
            ToastUtil.show("报价成功");
            info.setQuoted_price(mTvQuotedPrice.getText().toString());
            info.setQuoted_create_time((int) System.currentTimeMillis() / 1000);
            listener.onClick(null);
        } else {
            ToastUtil.show(dataBean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    public void setMyClickListener(@Nullable MyClickListener l) {
        this.listener = l;
    }

    private BasePopupView basePopupView;
    private BottomSelectPhoneDialog selectPhoneDialog;
    private void bottomSelect() {
        if (basePopupView == null) {
            selectPhoneDialog = new BottomSelectPhoneDialog(getContext(),info);
            basePopupView = new XPopup.Builder(getContext())
                    .asCustom(selectPhoneDialog)
                    .show();
        } else {
            basePopupView.show();
        }
    }

}
